package com.compus.controller;

import com.compus.constant.EnableStatus;
import com.compus.entity.po.Car;
import com.compus.entity.po.CarPosition;
import com.compus.entity.vo.CarPositionVo;
import com.compus.entity.vo.PositionUserCountVo;
import com.compus.entity.vo.Result;
import com.compus.service.CarPositionService;
import com.compus.service.CarService;
import com.compus.service.PositionService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RequestMapping("/driver")
@RestController
public class DriverController {

    @Resource
    private PositionService positionService;
    @Resource
    private CarService carService;
    @Resource
    private CarPositionService carPositionService;

    @Operation(summary = "获取全部站点信息以及站点用户数")
    @GetMapping("/getPositionAndUserCount")
    public Result<List<PositionUserCountVo>> getPositionAndUserCount(Long carId) {
        return Result.success(positionService.getPositionAndUserCount(carId));
    }

    @Operation(summary = "获取校车位置列表")
    @GetMapping("/getCarPositionList")
    public Result<List<CarPositionVo>> list(Long carId) {
        List<Car> list = carService.lambdaQuery()
                .eq(Car::getStatus, EnableStatus.ENABLE)
                .ne(Car::getId, carId)
                .list();
        ArrayList<CarPositionVo> positionVos = new ArrayList<>();
        for (Car car : list) {
            CarPosition position = carPositionService.lambdaQuery()
                    .eq(CarPosition::getCarId, car.getId())
                    .orderByDesc(CarPosition::getCreateTime)
                    .last("limit 1")
                    .one();
            if (position==null){
                continue;
            }
            CarPositionVo vo = new CarPositionVo();
            vo.setCarId(car.getId());
            vo.setCarPositionId(position.getId().toString());
            vo.setPosition(position.getPosition());
            positionVos.add(vo);
        }
        return Result.success(positionVos);
    }


}
